package com.winning.pmph.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winning.pmph.entity.KnowledgeLib;
import com.winning.pmph.entity.KnowledgeUnitParseKnowledgeTask;
import com.winning.pmph.entity.KnowledgeUnitParseKnowledgeTaskItem;
import com.winning.pmph.entity.Task;
import com.winning.pmph.mapper.KnowledgeUnitParseKnowledgeTaskMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class KnowledgeUnitParseKnowledgeTaskService extends ServiceImpl<KnowledgeUnitParseKnowledgeTaskMapper, KnowledgeUnitParseKnowledgeTask>{
    @Resource
    KnowledgeUnitParseKnowledgeTaskMapper knowledgeUnitParseKnowledgeTaskMapper;
    @Resource
    KnowledgeUnitParseKnowledgeTaskItemService knowledgeUnitParseKnowledgeTaskItemService;
    @Resource
    TaskService taskService;
    @Resource
    KnowledgeLibService knowledgeLibService;

    public Page selectHistoryPage(KnowledgeUnitParseKnowledgeTask task) {
        Page<KnowledgeUnitParseKnowledgeTask> page = new Page<>(task.getCurrentPage(), task.getPageSize());
        page = knowledgeUnitParseKnowledgeTaskMapper.selectTaskPage(task, page);
        for (KnowledgeUnitParseKnowledgeTask parseKnowledgeTask : page.getRecords()) {
            List<KnowledgeUnitParseKnowledgeTaskItem> taskItemList = knowledgeUnitParseKnowledgeTaskItemService.selectByTaskId(parseKnowledgeTask.getId());
            if (parseKnowledgeTask.getType() == 0) {
                String taskItemName = taskItemList.get(0).getTaskItemName();
                parseKnowledgeTask.setTaskName(parseKnowledgeTask.getTaskName() + "(" + taskItemName + ")");
            }
            else {
                Task processTask = taskService.getById(parseKnowledgeTask.getTaskId());
                KnowledgeLib knowledgeLib = knowledgeLibService.getById(processTask.getKnowledgeLibId());
                parseKnowledgeTask.setTaskName(parseKnowledgeTask.getTaskName() + "(" + knowledgeLib.getName() + "_" + taskItemList.size() + "条)");
            }
        }
        return page;
    }
}
